// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var layer = require("../../../component/layer/layer");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_detail_info = require("./financeApplicationList.css");
	var tools = require("tools");
	var constant = require("constant");
	var oData
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		$("#applyMain1").remove();
		$("#applyMain2").remove();
		oData = {
			pageIndex: pageIndex,
			pageSize: pageSize,
			totalPageCount: totalPageCount,
			totalRecordCount: totalRecordCount,
		};
		inquireFinanceList();
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		$("#financeApplicationList .navDiv a").unbind("click").click(function(){
			$("#financeApplicationList .navDiv a").removeClass("active");
			var financeState = $(this).attr("financeState").split(',');
			oData.financeState = financeState;
			oData.pageIndex = 1;
			
			$(this).addClass("active");
			inquireFinanceList();
		});
		
		//自动补齐
		$("#enterpriseName").on("keyup", function() {
			enterpriseNameAutocomplete();
		});
		
		/*$("#financeApplicationList").on("click", ".ui_button", function() {
			inquireEnterprisePageList();
		});*/
		
		$("#financeApplicationList").on("click", "#financeSearch", function() {
			var params = base.parseForm($("#financeApplicationList"));
			params.pageIndex = pageIndex;
			params.pageSize = pageSize;
			params.totalPageCount = totalPageCount;
			params.totalRecordCount = totalRecordCount;
			inquireFinanceList(params);
		});
		
		$("#financeApplicationList").on("click", ".detail", function() {
						var financeId = [];
						var $this = $(this).parents("tr.tr_style");
						financeId.push($this.attr("financeId"));
						router.route("financing/financingDetailInfo.html?financeId=" + financeId, true);
					});
		
	};

	//查询资信等级调整记录
	var inquireFinanceList = function(para) {
		if(para == undefined) {
			tools.loadList("inquireFinanceList.json", $("#financeApplicationList table"), $("#financeApplicationList .inquireFinanceListTpl"), oData, function(){
			
		}, null);
		}else {
			tools.loadList("inquireFinanceList.json", $("#financeApplicationList table"), $("#financeApplicationList .inquireFinanceListTpl"), para, function(){
			
		}, null);
		}
		
	};
	
	//查询企业名
	var inquireEnterprisePageList = function() {
		base.fnAjax({
			action: "inquireEnterprisePageList.json",
			async: false,
			nocode: true,
			oData: {
				pageSize: 50,
				sessionUuid: base.getData("sid"),
				appKey: base.getData("appKey"),
				enterpriseName: "",
				isPassed: true
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $(".showEnterpriseTpl").html();
					var html = juicer(tpl, data);
					$(".showEnterpriseName").html(html);
					
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};
	
	//公司名称自动补齐
	var enterpriseNameAutocomplete = function() {
		
		//自动补全
		$("#enterpriseName").autocomplete(seajs.data.handler + "inquireEnterprisePageList.json", {
			minChars: 1,
			max: 10,
			width: 200,
			matchCase: false, //不区分大小写
			scroll: false,
			dataType: 'json',
			scrollHeight: 500,
			//此处为传递而外的参数
			extraParams: {
				pageSize: 50,
				sessionUuid: base.getData("sid"),
				appKey: base.getData("appKey"),
				enterpriseName: function() {
					return $("#enterpriseName").val();
				},
				isPassed: true
			},
			//需要把data转换成json数据格式                      
			parse: function(data) {
				return $.map(eval(data.datas), function(row) {
					return {
						data: row,
						value: row.enterpriseName, //此处无需把全部列列出来，只是两个关键列
						result: row.enterpriseName
					}
				});
			},
			formatItem: function(data, i, total) {
				if ($("#enterpriseName").hasClass('nonemorelist')) {
					$("#enterpriseName").removeClass('nonemorelist');
					return;
				};
				return "<table><tr enterpriseId=" + data.enterpriseId + "><td>" + data.enterpriseName + "</td></tr></table>";
			},
			formatMatch: function(data, i, total) {
				return data.enterpriseName;
			},
			formatResult: function(data, value) {
				return data.enterpriseName;
			}
		}).result(function(event, data, formatted) { //回调
			if ($("#enterpriseName").hasClass('nonemorelist')) {
				$("#enterpriseName").removeClass('nonemorelist');
			} else {
				$("#enterpriseName").addClass('nonemorelist');
			};
		});
	};
	
	return {
		init: init
	};
});